package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.domain.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/9 19:27
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelePhone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public List<Long> findCountByMonths(List<String> monthsList) {
        List<Long> memberCount = new ArrayList<>();
        if (monthsList != null && monthsList.size() > 0) {
            for (String month : monthsList) {
                String date = month + ".31";
                Long count = memberDao.findMemberCountByDate(date);
                memberCount.add(count);
            }
        }
        return memberCount;
    }

    //运营数据统计
    @Override
    public Map getBusinessReportData() {
        Map map = new HashMap();
        //当天新增会员数;
        Long todayNewMember = memberDao.getTodayNewMember();
        map.put("todayNewMember", todayNewMember);
        //总会员数;
        Long totalMember = memberDao.getTotalMember();
        map.put("totalMember", totalMember);
        //本周新增会员数;
        Long thisWeekNewMember = memberDao.getThisWeekNewMember();
        map.put("thisWeekNewMember", thisWeekNewMember);
        //本月新增会员数;
        Long thisMonthNewMember = memberDao.getThisMonthNewMember();
        map.put("thisMonthNewMember", thisMonthNewMember);
        //今日预约数;
        Long todayOrderNumber = orderDao.getTodayOrderNumber();
        map.put("todayOrderNumber", todayOrderNumber);
        //今日到诊数;
        Long todayVisitsNumber = orderDao.getTodayVisitsNumber();
        map.put("todayVisitsNumber", todayVisitsNumber);
        //本周预约数;
        Long thisWeekOrderNumber = orderDao.getThisWeekOrderNumber();
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        //本周到诊数;
        Long thisWeekVisitsNumber = orderDao.getThisWeekVisitsNumber();
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        //本月预约数;
        Long thisMonthOrderNumber = orderDao.getThisMonthOrderNumber();
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        //本月到诊数;
        Long thisMonthVisitsNumber = orderDao.getThisMonthVisitsNumber();
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        //热门套餐;
        List<Map> setmealList = orderDao.getSetmealList();
        map.put("setmealList", setmealList);
        return map;
    }

    //性别比例查询;
    @Override
    public Map genderCount() {
        Map resultMap = new HashMap();
        List<String> genderList = new ArrayList<>();
        List<Map> genderCount = new ArrayList<>();
        List<Map> list = memberDao.findGenderCount();
        for (Map map : list) {
            Map temp = new HashMap();
            Object sex = map.get("sex");
            System.out.println(sex);
            Integer gender = Integer.valueOf(map.get("sex").toString());
            if (gender == 0) {
                genderList.add("未知");
                temp.put("name", "未知");
                temp.put("value", map.get("count"));
            } else if (gender == 1) {
                genderList.add("男");
                temp.put("name", "男");
                temp.put("value", map.get("count"));
            } else if (gender == 2) {
                genderList.add("女");
                temp.put("name", "女");
                temp.put("value", map.get("count"));
            }
            genderCount.add(temp);
        }
        resultMap.put("genderList", genderList);
        resultMap.put("genderCount", genderCount);
        return resultMap;
    }

    //查询套餐预约占比;
    @Override
    public Map getSetmealReport() {
        Map map = new HashMap();
        //1.查询套餐列表及套餐占比
        List<Map> setmealCountList = orderDao.getSetmealReport();
        //[{name=套餐名,value=数量}]
        map.put("setmealCountList", setmealCountList);
        //2.获取套餐名称集合
        if (setmealCountList != null && setmealCountList.size() > 0) {
            List<String> setmealList = new ArrayList<>();
            //获取套餐的名称;
            for (Map temp : setmealCountList) {
                String setmealName = temp.get("name").toString();
                setmealList.add(setmealName);
            }
        }
        return map;
    }
}
